<?php

namespace frontend\modules\assist\models;

use Yii;
use yii\data\ActiveDataProvider;

/**
 * 帮助数据模型
 *
 * @author emhome <emhome@163.com>
 * @since 1.0
 */
class HelpSearch extends \common\models\help\Help {

    /**
     * 列表查询
     * @return ActiveDataProvider
     */
    public function search() {
        $query = static::find()
        ->active(static::STATUS_ACTIVE)
        ->andWhere(['category_id' => $this->category_id])
        ->orderBy([
            'listorder' => SORT_ASC,
            'created_at' => SORT_DESC
        ]);

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        Yii::$app->db->cache(function($db) use($dataProvider) {
            $dataProvider->prepare();
        }, 60);

        return $dataProvider;
    }

    public function fields() {
        return [
            'id',
            'title',
            'created_at' => function () {
                return date('Y-m-d', $this->created_at);
            }
        ];
    }

}
